/**
 * 
 */
package edu.hm.compiler.parser.grammar.term;

import java.util.Collections;
import java.util.List;

import edu.hm.compiler.parser.grammar.Symbol;
import edu.hm.compiler.parser.grammar.variable.Variable;

/**
 * @author Saruman
 */
public class TermCall implements Term
{
    private final Symbol       symbol;
    private final List< Term > termList;

    /**
     * @param symbol
     */
    public TermCall( final Symbol symbol, final List< Term > termList )
    {
        this.symbol = symbol;
        this.termList = Collections.unmodifiableList( termList );
    }

    public Symbol getSymbol()
    {
        return this.symbol;
    }

    public List< Term > getTermList()
    {
        return this.termList;
    }

    @Override
    public String toString()
    {
        return "TermCall [symbol=" + this.symbol + ", termList=" + this.termList + "]"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#hashCode()
     */
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ( ( this.symbol == null ) ? 0 : this.symbol.hashCode() );
        result = prime * result + ( ( this.termList == null ) ? 0 : this.termList.hashCode() );
        return result;
    }

    /*
     * (non-Javadoc)
     * @see java.lang.Object#equals(java.lang.Object)
     */
    @Override
    public boolean equals( final Object obj )
    {
        if( this == obj )
            return true;
        if( obj == null )
            return false;
        if( obj instanceof Variable )
            return true;
        if( this.getClass() != obj.getClass() )
            return false;
        final TermCall other = (TermCall) obj;
        if( this.symbol == null )
        {
            if( other.symbol != null )
                return false;
        } else if( !this.symbol.equals( other.symbol ) )
            return false;
        if( this.termList == null )
        {
            if( other.termList != null )
                return false;
        } else if( !this.termList.equals( other.termList ) )
            return false;
        return true;
    }

    @Override
    public String getString()
    {
        final StringBuilder b = new StringBuilder( this.symbol.getConsId() );
        b.append( '(' );
        b.append( this.termList.get( 0 ).getString() );
        for( int i = 1; i < this.termList.size(); i++ )
            b.append( ", " ).append( this.termList.get( i ).getString() ); //$NON-NLS-1$
        b.append( ')' );
        return b.toString();
    }
}
